﻿
Namespace LightSwitchApplication

    Public Class MyAppointments

        Private Sub CurrentAppointments_Loaded(succeeded As Boolean)
            If succeeded Then
                If Me.CurrentAppointments Is Nothing OrElse Me.CurrentAppointments.Count = 0 Then
                    If ShowMessageBox("You do not have any upcoming appointments. Would you like to add one now?", "My Appointments", MessageBoxOption.YesNo) = Windows.MessageBoxResult.Yes Then
                        Me.CurrentAppointments.AddNew()
                        AddasCurrentUser()
                    End If
                End If
            End If

        End Sub


        Private Sub ShowMap_CanExecute(ByRef result As Boolean)
            result = (Me.CurrentAppointments IsNot Nothing AndAlso
                      Me.CurrentAppointments.SelectedItem IsNot Nothing AndAlso
                      Me.CurrentAppointments.SelectedItem.Location <> "")
        End Sub
        Private Sub ShowMap_Execute()
            ' Write your code here.
            Me.Application.ShowMapScreen(Me.CurrentAppointments.SelectedItem.Location)
        End Sub

        Private Sub MyAppointments_InitializeDataWorkspace(saveChangesTo As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService))
            ' Write your code here.
            Me.Text_ScreenTitle = "My Appointments"
            Me.Text_EmailNotification = "* Please note that an email will be sent if you add, modify or cancel your already scheduled appointments."
            Me.currentUser = Me.Application.User.Name
        End Sub

      

        Private Sub UpcomingAppointmentsAddAndEditNew_Execute()
            ' Write your code here.
            Me.CurrentAppointments.AddNew()
            AddasCurrentUser()
            Me.OpenModalWindow("CustomerPopup")
        End Sub

        Private Sub UpcomingAppointmentsDeleteSelected_CanExecute(ByRef result As Boolean)
            ' Write your code here.
            result = (Me.CurrentAppointments IsNot Nothing AndAlso Me.CurrentAppointments.SelectedItem IsNot Nothing)
        End Sub

        Private Sub UpcomingAppointmentsDeleteSelected_Execute()
            ' Write your code here.
            If Me.CurrentAppointments.SelectedItem.Details.EntityState = EntityState.Added Then
                'Just delete right away if this was just added
                Me.CurrentAppointments.SelectedItem.Delete()
            Else
                'Otherwise confirm with the user that this appointment should be canceled. (An email update will be sent)
                If ShowMessageBox("Are you sure you want to cancel this appointment? An email update will be sent.", "Cancel Appointment", MessageBoxOption.YesNo) = Windows.MessageBoxResult.Yes Then
                    Me.CurrentAppointments.SelectedItem.Delete()
                End If
            End If
        End Sub

        Sub AddasCurrentUser()
            Dim userDetails = Me.DataWorkspace.ApplicationData.GetUserByUsername(Me.Application.User.Name)
            Me.CurrentAppointments.SelectedItem.User = userDetails.FirstOrDefault
        End Sub
    End Class

End Namespace
